/**
 * Http 请求
 *
 * @author liuqi
 * @date 2024/12/21 17:38
 **/
import AxiosRequest from "./AxiosRequest";
import tools from "@/util/Tools";
const request = {

    /**
     * 发送get请求
     *
     * @param url       url
     * @param params    入参
     */
    get: function (url, params) {
        let config = {
            url: url,
            params: params,
            method: "GET",
        }
        return this.request(config);
    },

    /**
     * 发送delete请求
     *
     * @param url
     * @param params
     */
    delete: function (url, params) {
        let config = {
            url: url,
            params: params,
            method: "DELETE",
        }
        return this.request(config);
    },

    /**
     * 发送post请求, content-type=application/json
     *
     * @param url       url
     * @param data      入参
     */
    post: function (url, data) {
        let config = {
            url: url,
            data: data,
            method: "POST",
        }
        return this.request(config);
    },

    /**
     * 发送post请求, content-type=application/x-www-form-urlencoded
     *
     * @param url
     * @param data
     */
    postForm: function (url, data) {
        let config = {
            url: url,
            data: tools.httpQuery(data),
            headers: {"Content-Type": "application/x-www-form-urlencoded"},
            method: "POST",
        }
        return this.request(config);
    },

    /**
     * @param config 参考: AxiosRequestConfig
     */
    request: function (config) {
        if (tools.isEmpty(config.url)) throw new Error("request [url] is empty");
        return new AxiosRequest(config).send();
    }
}
export default request;
